<?php 
if(!($_GET['print'] > 0))
{
	$buttons_l[0]['onmousedown'] = 'edit_form.action = \'print.php'.$commander->getFormAction().'&amp;print=1\'; edit_form.target=\'_blank\';';
	$buttons_l[0]['onmouseup'] = 'edit_form.submit();';
	$buttons_l[0]['image'] = 'print_22.gif';
	
	$buttons_l[1]['image'] = 'tip_22.gif';
	$buttons_l[1]['id'] = 'print_help';
	
	echo $commander->getListCommandsPanel('', '', $buttons_l); 
}
$demogs = false;
?>
<?php if($surveyAdmin->Survey->Demogs): ?>
<table>
    <?php 
        foreach($surveyAdmin->Survey->Demogs as $demog)
        {
			if($demog->Values)
			{
            ?>
            	<tr>
                    <td align="left"><?php echo $demog->Title; ?>&nbsp;&nbsp;</td>
                    <td align="left">
                    	<?php if(!($_GET['print'] > 0)): ?>
                        <select name="demog[<?php echo $demog->Id; ?>]" onchange="submit();">
                            <option value="0">Alle</option>
                            <?php endif; ?>
                            <?php 
                            foreach($demog->Values as $value)
                            { 
							?> 
                            	<?php if(!($_GET['print'] > 0)): ?>
                            	<option value="<?php echo $value->Id; ?>" <?php echo getIsSelectedHTML($value->Id, $_REQUEST['demog'][$demog->Id]); ?>>
                                <?php endif; ?>
								<?php 
								if($_GET['print'] > 0)
								{
									if($value->Id == $_REQUEST['demog'][$demog->Id])
									{ echo $value->Value; }
								}
								else
								{ 
									echo $value->Value;  
									echo '   ('.$surveyAdmin->Survey->getResponsesDemogCount($value->Id).' '.getWord('responses').' )';
								}
								?>
                                <?php if(!($_GET['print'] > 0)): ?>
                                </option> 
                                <?php endif; ?>
							<?php  
							}
                            if($_REQUEST['demog'][$demog->Id] > 0)
                            { $demogs[$demog->Id] = $_REQUEST['demog'][$demog->Id]; }
                            ?>
                        <?php if(!($_GET['print'] > 0)): ?>
                        </select>
                        <?php else: 
						if(!($_REQUEST['demog'][$demog->Id] > 0))
						{ echo 'Alle'; }
						?>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php 
			}
        }
    ?>
</table>
<?php endif; ?>
<div align="left" class="report statistics page <?php if($_GET['print'] > 0){ echo 'print'; } ?>">
<center>
<h1><?php echo $surveyAdmin->Survey->Title; ?></h1>
<h2><?php echo ucfirst($txt->getWord('summary')); ?></h2>
<h2><?php 
	if($demogs)
	{ echo $surveyAdmin->Survey->getResponsesDemogsCount($demogs); }
	else
	{ echo $surveyAdmin->Survey->getResponsesCount(); }
	echo '&nbsp;'.getWord('responses');
	?></h2>
</center>
</div>
<?php 
if($surveyAdmin->Survey->Sections)
{
	$i = 1;
	foreach($surveyAdmin->Survey->Sections as $section)
	{
		if(($section->Active) && ($section->ActiveQuestions > 0))
		{
			?>
            <div align="left" class="report statistics page section <?php if($_GET['print'] > 0){ echo 'print'; } ?>">
            <h2><?php echo $i; ?>.&nbsp;&nbsp;<?php echo $section->Title;  ?></h2>
            <p><?php echo $section->Guide; ?></p>
            <?php 
			if($section->Questions)
			{
				$j=1;
				$xAxis=1;
				$ticks = '';
				$ticks2y = '';
				?>
                <table class="editor section sec_<?php echo $section->Id; ?>" cellpadding="0" border="0" cellspacing="0">
                <tbody>
                    <?php 
                    foreach($section->Questions as $question)
                    {
						if($question->Active)
						{
							if($ticks != '')
							{ $ticks .= ', '; }
							$ticks .= '[-'.$j.', "'.$j.'.&nbsp;'.substr($question->Question,0,90).'"]';
							if($question->MinValue <= $question->MaxValue)
							{
								for($k = $question->MinValue; $k<=$question->MaxValue; $k++)
								{ 
									if($xAxis < $k)
									{ $xAxis=$k; }
								}
								$sumtotal = 0;
								$totalcount = 0;
								$count = 0;
								$average[$j][0] = 0;
								for($k = $question->MinValue; $k<=$question->MaxValue; $k++)
								{	
									$count = $question->getAnsweredValueCount($k);
									$total[$k] = ($count*$k);
									$sumtotal += $total[$k]; 
									$totalcount += $count;
								}
								if($totalcount > 0)
								{ $average[$j][0] = number_format(($sumtotal/$totalcount),2); }
								if($section->Type == 0)
								{
									$sumtotal = 0;
									$totalcount = 0;
									$count = 0;
									$average[$j][1] = 0;
									for($k = $question->MinValue; $k<=$question->MaxValue; $k++)
									{
										$count = $question->getAnsweredValueCount($k, 'value2');
										$total[$k] = ($count*$k);
										$sumtotal += $total[$k]; 
										$totalcount += $count;
									}
									if($totalcount > 0)
									{ $average[$j][1] = number_format(($sumtotal/$totalcount),2); }
								}
							}
							if($ticks2y != '')
							{ $ticks2y .= ', '; }
							$ticks2y .= '[-'.$j.', "<strong>&nbsp;&nbsp;G:'.$average[$j][0];
							if($section->Type == 0)
							{ $ticks2y .= '<br />&nbsp;&nbsp;I:'.$average[$j][1]; }
							$ticks2y .= '</strong>"]';
							$j++;
						}
                    }
                    ?>
                    <tr>
                    	<td colspan="4" align="right"><div align="right" id="legend<?php echo $i; ?>"></div><br /><br /></td>
                    </tr>
                    <tr>
                    	<td colspan="4">
                        <div id="placeholder_demog<?php echo $i; ?>" style="width:595px; height:<?php echo ($j*40)+40 ?>px;"></div>
                        </td>
                    </tr>
                </tbody>
                </table>
                <?php
			}
			?>
            
			<script type="text/javascript">
			jQuery(document).ready(function()
			{
				<?php 
				$d1 = '';
				$d2 = '';
				//$d3 = '';
				$ticks = ereg_replace("\r\n", "", $ticks);
				$ticks = ereg_replace("\n", "", $ticks);
				if($j > 0)
				{
					for($k=1; $k<=$j; $k++)
					{
						if(isset($average[$k][0]))
						{
							if($d1 != '')
							{ $d1 .= ', '; }
							$d1 .= '['.$average[$k][0].', -'.$k.']';
						}
						if(isset($average[$k][1]))
						{
							if($d2 != '')
							{ $d2 .= ', '; }
							$d2 .= '['.$average[$k][1].', -'.$k.']';
							
							if(isset($average[$k][0]))
							{
								if($average[$k][0] > $average[$k][1])
								{ $average[$k][2] = ($average[$k][0] - $average[$k][1]); }
								else
								{ $average[$k][2] = ($average[$k][1] - $average[$k][0]); }
								
								//if($d3 != '')
								//{ $d3 .= ', '; }
								//$d3 .= '['.$average[$k][2].', -'.$k.']';
							}
						}
					}
				}
				?>
				var d1 = [null];
				var d2 = [null];
				//var d3 = [null];
				<?php if($d1 != ''): ?>
				var d1 = [<?php echo $d1; ?>];
				<?php endif; ?>
				<?php if($d2 != ''): ?>
				var d2 = [<?php echo $d2; ?>];
				<?php endif; ?>
				<?php if($d3 != ''): ?>
				//var d3 = [<?php echo $d3; ?>];
				<?php endif; ?>
				jQuery.plot(jQuery("#placeholder_demog<?php echo $i; ?>"), [
				{ 
					label: "Gjennomsnitt Alle", 
					lines: { show: true },
    				points: { show: true },
					data: d1 
				}
				,{
					<?php if($d2 != ''): ?>
					label: "Ønsket situasjon", 
					<?php endif; ?>
					lines: { show: true },
    				points: { show: true },
					yaxis:2,
					data: d2
				}
				<?php if($d3 != ''): ?>
//				,{
//					label: "Differansen", 
//					lines: { show: true },
//   				points: { show: true },
//					data: d3
//				}
				<?php endif; ?>
				], 
				{ 
					yaxis: 
					{ 
						tickSize: 1,
						ticks: [<?php echo $ticks; ?>],
						labelWidth:300,
						max:-1, 
						min: -<?php echo count($section->Questions); ?>
					}, 
					y2axis: 
					{
						tickSize: 1,
						ticks: [<?php echo $ticks2y; ?>],
						max:-1, 
						min: -<?php echo count($section->Questions); ?>
					},
					xaxis:  
					{ 
						tickSize: 1,
						min: 1, 
						max: <?php echo $xAxis; ?> 
					},
					legend: 
					{ 
						position: 'ne',
						backgroundOpacity:0.4,
						container:jQuery("#legend<?php echo $i; ?>")
					},
					grid: {
						//color: color,
						backgroundColor:null,
						//tickColor: color
						//labelMargin: number
						//markings: array of markings or (fn: axes -> array of markings)
						borderWidth:0,
						//clickable:true,
						//hoverable:true,
						//autoHighlight:true,
						//mouseActiveRadius:4
					  }

				});
			});
			</script>
            </div>
            <?php
			$i++;
		}
	}
}
?>